app.controller('AgencyOrderCtrl', function ($scope, ajax, $modal, toaster, $filter, $rootScope, authorities) {

	$scope.agencyList = [];
	$scope.starList = [];
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.flag = false;

	$scope.reload = $scope.page.loader(
		'agency/{userId}/{pageIndex}/{pageSize}/orders'.format({ userId: $rootScope.loginUser.id }));
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);

	$scope.checkQueryMap = function () {
		if ($scope.statusList.selected) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else {
			delete $scope.queryMap.status;
		}
		if ($scope.starList.selected) {
			$scope.queryMap.orderDetail__startStation = $scope.starList.selected.name;
		} else {
			delete $scope.queryMap.orderDetail__startStation;
		}
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else {
			delete $scope.queryMap.createTime;
		}
		if ($scope.agencyList.selected) {
			$scope.queryMap.agencyId = $scope.agencyList.selected.id;
		} else {
			delete $scope.queryMap.agencyId;
		}
	};

	$scope.query = function () {
		$scope.checkQueryMap();
		$scope.reload(1, $scope.queryMap);
	};

	$scope.total = function () {
		$scope.checkQueryMap();
		$scope.reload(1, $scope.queryMap);
		ajax.call(new Req.POST({
			fields: $scope.queryMap,
			resource: 'agency/{userId}/summary'.format({ userId: $rootScope.loginUser.id })
		})).then(function (result) {
			if (result && result.code == 200) {
				$scope.summary = result.result;
				$scope.flag = true;
			} else {
				if (result.code != 423) toaster.pop('error', '错误', result.result);
				$scope.flag = false;
			}
		}, function () {
			toaster.pop('error', '错误', '无法连接到服务器！');
		});
	};

	//导出Excel
	$scope.export = function () {
		$scope.checkQueryMap();
		ajax.call(new Req.POST({
			fields: $scope.queryMap,
			resource: 'agency/{userId}/export'.format({ userId: $rootScope.loginUser.id })
		})).then(function (result) {
			if (result.code == 200) {
				window.open(result.result);
				toaster.pop('success', '成功', '导出信息成功。');
			} else {
				if (result.code == 423) toaster.pop('warning', '提醒', result.result);
				else toaster.pop('error', '错误', '导出信息失败！！');
			}
		}, function () { toaster.pop('error', '错误', '导出信息失败！'); });
	};

	//订单状态
	$scope.statusList = [
		{ id: 1, name: "待付款" },
		{ id: 2, name: "支付中" },
		{ id: 3, name: "已支付" },
		{ id: 4, name: "已完成" },
		{ id: 5, name: "已取消" },
		{ id: 6, name: "订单超时" },
		{ id: 7, name: "退款中" },
		{ id: 8, name: "退款完成" },
		{ id: 9, name: "退款失败" }
	];
	//起始客运站列表读取
	ajax.get('starting/bk/list', function (result) {
		if (result && result.code == 200) $scope.starList = result.result;
	}, function () { });
	if (authorities.hasAuthority('BOOKING')) {
		// 代售点列表
		ajax.get('agency/list', function (result) {
			if (result && result.code == 200) $scope.agencyList = result.result;
		}, function () { });
	}
});